﻿Shader "Custom/TestShader27"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _WaveMap("Bump", 2D) = "bump" {}
        _Cubemap("Cube", Cube) = "_Skybox" {}
        _WaveXSpeed("WaveXSpeed", range(-0.1, 0.1)) = 0.01
        _WaveYSpeed("WaveYSpeed", range(-0.1, 0.1)) = 0.01
        _Distortion("Distortion", range(0, 100)) = 10
    }

    SubShader
    {
        Tags{ "Queue" = "Transparent" "RenderType" = "Opaque" }
        GrabPass{ "_RefractionTex" }
        Pass
        {
            Tags{ "LightMode" = "ForwardBase" }
            CGPROGRAM
            #pragma multi_compile_fwdbase
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _MainTex_TexelSize;

            sampler2D _WaveMap;
            float4 _WaveMap_ST;
            float4 _WaveMap_TexelSize;

            samplerCUBE _Cubemap;

            float _WaveXSpeed;
            float _WaveYSpeed;
            float _Distortion;

            sampler2D _RefractionTex;
            float4 _RefractionTex_TexelSize;

            struct a2v
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
            };


            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float4 TtoW0 : TEXCOORD1;
                float4 TtoW1 : TEXCOORD2;
                float4 TtoW2 : TEXCOORD3;
                float4 scrPos : TEXCOORD4;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.texcoord.xy * _WaveMap_ST.xy + _WaveMap_ST.zw;
                o.scrPos = ComputeGrabScreenPos(o.pos);

                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
                float3 worldDinormal = normalize(cross(worldNormal, worldTangent) * v.tangent.w);
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                o.TtoW0 = float4(worldTangent.x, worldDinormal.x, worldNormal.x, worldPos.x);
                o.TtoW1 = float4(worldTangent.y, worldDinormal.y, worldNormal.y, worldPos.y);
                o.TtoW2 = float4(worldTangent.z, worldDinormal.z, worldNormal.z, worldPos.z);

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
                float3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));

                float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);

                fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;
                fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;
                fixed3 bump = normalize(bump1 + bump2);

                float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
                i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
                fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy / i.scrPos.w).rgb;

                bump = normalize(float3(dot(bump, i.TtoW0.xyz), dot(bump, i.TtoW1.xyz), dot(bump, i.TtoW2.xyz)));

                fixed3 texColor = tex2D(_MainTex, i.uv.xy + speed).rbg;

                fixed3 reflDir = reflect(-viewDir, bump);
                fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor * _Color.rgb;

                fixed fresnel = pow(1 - saturate(dot(viewDir, bump)), 4);
                fixed3 finalColor = reflCol * fresnel + refrCol * (1 - fresnel);
                return fixed4(finalColor, 1);
            }
            ENDCG
        }
    }
}
